/**
 * 
 */
package com.unsanctioned.radioagregator.player;

import java.io.IOException;

import io.vov.vitamio.MediaPlayer;

/**
 * @author wizard
 * This player uses Vitamio engine. All right reserved to Vitamio creator.
 */
public class Player {
	public static final String DOGTAG = Player.class.getName();
	public static final int START_PLAYING = 0, STOP_PLAYING = 1, ERROR_PLAYING = 999, ERROR_STOPPING = 888;
	private static Player instance;
	private MediaPlayer mediaplayer;
	public Player(MediaPlayer mediaPlayer) {
		this.mediaplayer = mediaPlayer;
		this.instance = this;
	}
	public static Player getPlayer(MediaPlayer mediaPlayer) {
		if (instance != null) {
			return instance;
		} else {
			return new Player(mediaPlayer);
		}
	} 
	public static Player getPlayer() {
		return instance;
	}
	public int stream(String url) {
		try {
			mediaplayer.setDataSource(url);
			mediaplayer.prepareAsync();
			mediaplayer.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (mediaplayer.isPlaying()) {
			return START_PLAYING;
		} else {
			return ERROR_PLAYING;
		}
	}
	public int stop() {
		try {
			if (mediaplayer.isPlaying()) {
				mediaplayer.stop();
			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
		if (mediaplayer.isPlaying()) {
			return ERROR_STOPPING;
		} else {
			return STOP_PLAYING;
		}
	}
}
